package com.JDK.Lock;


import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

public class CountDown2 {

    static final CountDownLatch latch1 = new CountDownLatch(1);//裁判就位同步器

    static final CountDownLatch latch2 = new CountDownLatch(4);//终点

    static ArrayList<String> ans = new ArrayList<>();

    public static void main(String[] args) {
        for(int i=0; i<4; i++){
            new Thread(() -> {
                try{
                    System.out.println("运动员到起跑线等待裁判发令");
                    latch1.await();
                    System.out.println("口令响起了！！");
                    Thread.sleep((long) (Math.random() * 10000)); // 模拟跑步
                    latch2.countDown();
                    ans.add(Thread.currentThread().getName());
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
            },"Thread - " + i).start();
        }

        try{
            Thread.sleep((long) (Math.random() * 10000));
            System.out.println("裁判已经就位，即将发布口令");
            latch1.countDown();
            System.out.println("裁判已经发布口令，等待选手冲过重点");
            latch2.await();
            System.out.println("所有选手到达终点");
            System.out.println("选手的名次为: ");
            ans.forEach(System.out::println);
        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }

}






